package com.todo.biz.application.convert;


import com.google.api.client.util.Lists;
import com.todo.biz.application.entity.bo.CollectRelationBO;
import com.todo.biz.application.entity.bo.CollectSetBO;
import com.todo.biz.application.entity.condition.CollectSetConditionBO;
import com.todo.biz.common.enums.CollectSetTypeEnum;
import com.todo.biz.iface.backend.request.InsertCollectSetReq;
import com.todo.biz.iface.backend.request.QueryCollectSetByConditionReq;
import com.todo.biz.iface.backend.request.UpdateCollectRelationReq;
import com.todo.biz.iface.backend.request.UpdateCollectSetByIdReq;
import com.todo.biz.iface.backend.response.QueryCollectSetByConditionResp;
import com.todo.biz.iface.backend.response.QueryCollectSetDetailResp;
import com.todo.biz.infrastructure.entity.condition.CollectSetCondition;
import com.todo.biz.infrastructure.entity.po.CollectRelationPO;
import com.todo.biz.infrastructure.entity.po.CollectSetPO;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.utils.TodoCommonUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/4/20 11:32
 * @description
 */
public class CollectConvert {
    public static CollectSetBO convertToCollectSetBO(CollectSetPO collectSetPO) {
        if (collectSetPO == null) {
            return null;
        }
        CollectSetBO collectSetBO = new CollectSetBO();
        collectSetBO.setCollectRule(collectSetPO.getCollectRule());
        collectSetBO.setCollectType(CollectSetTypeEnum.getByType(collectSetPO.getCollectType()));
        collectSetBO.setCollectCount(collectSetPO.getCollectCount());
        collectSetBO.setCity(collectSetPO.getCity());
        collectSetBO.setCode(collectSetPO.getCode());
        collectSetBO.setStatus(EnableTypeEnum.getByType(collectSetPO.getStatus()));
        collectSetBO.setCreateUser(collectSetPO.getCreateUser());
        collectSetBO.setCreateUser(collectSetPO.getCreateUser());
        collectSetBO.setDesc(collectSetPO.getDesc());
        collectSetBO.setId(collectSetPO.getId());
        collectSetBO.setLastUpdateUser(collectSetPO.getLastUpdateUser());
        collectSetBO.setMainImg(collectSetPO.getMainImg());
        collectSetBO.setProvince(collectSetPO.getProvince());
        collectSetBO.setTitle(collectSetPO.getTitle());
        return collectSetBO;
    }

    public static CollectSetBO convertToCollectSetBO(InsertCollectSetReq req) {
        if (req == null) {
            return null;
        }
        CollectSetBO collectSetBO = new CollectSetBO();
        collectSetBO.setCollectRule(req.getCollectRule());
        collectSetBO.setCollectType(CollectSetTypeEnum.getByType(req.getCollectType()));
        collectSetBO.setCollectCount(0);
        collectSetBO.setCity(req.getCity());
        collectSetBO.setCode(req.getCode());
        collectSetBO.setStatus(EnableTypeEnum.FORBIDDEN);
        collectSetBO.setCreateUser(req.get_staff().getUsername());
        collectSetBO.setDesc(req.getDesc());
        collectSetBO.setId(TodoCommonUtils.buildLongGuid());
        collectSetBO.setLastUpdateUser(req.get_staff().getUsername());
        collectSetBO.setMainImg(req.getMainImg());
        collectSetBO.setProvince(req.getProvince());
        collectSetBO.setTitle(req.getTitle());
        return collectSetBO;
    }

    public static CollectSetBO convertToCollectSetBO(UpdateCollectSetByIdReq req) {
        if (req == null) {
            return null;
        }
        CollectSetBO collectSetBO = new CollectSetBO();
        collectSetBO.setCollectRule(req.getCollectRule());
        collectSetBO.setCollectType(CollectSetTypeEnum.getByType(req.getCollectType()));
        collectSetBO.setCollectCount(0);
        collectSetBO.setCity(req.getCity());
        collectSetBO.setCode(req.getCode());
        collectSetBO.setStatus(EnableTypeEnum.FORBIDDEN);
        collectSetBO.setCreateUser(req.get_staff().getUsername());
        collectSetBO.setDesc(req.getDesc());
        collectSetBO.setId(req.getId());
        collectSetBO.setLastUpdateUser(req.get_staff().getUsername());
        collectSetBO.setMainImg(req.getMainImg());
        collectSetBO.setProvince(req.getProvince());
        collectSetBO.setTitle(req.getTitle());
        return collectSetBO;
    }


    public static CollectSetPO convertToCollectSetPO(CollectSetBO collectSetBO) {
        if (collectSetBO == null) {
            return null;
        }
        CollectSetPO collectSetPO = new CollectSetPO();
        collectSetPO.setCollectRule(collectSetBO.getCollectRule());
        collectSetPO.setCollectType(collectSetBO.getCollectType() == null ? null : collectSetBO.getCollectType().getType());
        collectSetPO.setCollectCount(collectSetBO.getCollectCount());
        collectSetPO.setCity(collectSetBO.getCity());
        collectSetPO.setCode(collectSetBO.getCode());
        collectSetPO.setStatus(collectSetBO.getStatus() == null ? EnableTypeEnum.FORBIDDEN.getType() : collectSetBO.getStatus().getType());
        collectSetPO.setCreateUser(collectSetBO.getCreateUser());
        collectSetPO.setDesc(collectSetBO.getDesc());
        collectSetPO.setId(collectSetBO.getId());
        collectSetPO.setLastUpdateUser(collectSetBO.getLastUpdateUser());
        collectSetPO.setMainImg(collectSetBO.getMainImg());
        collectSetPO.setProvince(collectSetBO.getProvince());
        collectSetPO.setTitle(collectSetBO.getTitle());
        collectSetPO.setIsDelete(0);
        collectSetPO.setCreateTime(new Date());
        collectSetPO.setUpdateTime(new Date());
        return collectSetPO;
    }

    public static CollectSetCondition convertToCollectSetCondition(CollectSetConditionBO conditionBO) {
        if (conditionBO == null) {
            return null;
        }
        CollectSetCondition condition = new CollectSetCondition();
        condition.setCity(conditionBO.getCity());
        condition.setCollectType(conditionBO.getCollectType() == null ? null : conditionBO.getCollectType().getType());
        condition.setCreateUser(conditionBO.getCreateUser());
        condition.setPageIndex(conditionBO.getPageIndex());
        condition.setPageSize(conditionBO.getPageSize());
        condition.setTitle(conditionBO.getTitle());
        condition.setProvince(conditionBO.getProvince());
        condition.setCurrentIndex(conditionBO.getCurrentIndex());
        return condition;
    }

    public static CollectRelationPO convertToCollectRelationPO(CollectRelationBO collectRelationBO) {
        if (collectRelationBO == null) {
            return null;
        }
        CollectRelationPO collectRelationPO = new CollectRelationPO();
        collectRelationPO.setIsDelete(0);
        collectRelationPO.setRelationId(collectRelationBO.getRelationId());
        collectRelationPO.setRelationType(collectRelationBO.getRelationType() == null ? null : collectRelationBO.getRelationType().getType());
        collectRelationPO.setSetId(collectRelationBO.getSetId());
        collectRelationPO.setSort(collectRelationBO.getSort());
        collectRelationPO.setId(collectRelationBO.getId() == null ? TodoCommonUtils.buildLongGuid() : collectRelationBO.getId());
        collectRelationPO.setUpdateTime(new Date());
        collectRelationPO.setCreateTime(new Date());
        return collectRelationPO;
    }

    public static CollectRelationBO convertToCollectRelationBO(CollectRelationPO collectRelationPO) {
        if (collectRelationPO == null) {
            return null;
        }
        CollectRelationBO collectRelationBO = new CollectRelationBO();
        collectRelationBO.setId(collectRelationPO.getId());
        collectRelationBO.setRelationId(collectRelationPO.getRelationId());
        collectRelationBO.setRelationType(CollectSetTypeEnum.getByType(collectRelationPO.getRelationType()));
        collectRelationBO.setSetId(collectRelationPO.getSetId());
        collectRelationBO.setSort(collectRelationPO.getSort());
        return collectRelationBO;
    }

    public static List<CollectRelationPO> convertToCollectRelationPOList(List<CollectRelationBO> collect) {
        if (collect == null) {
            return Lists.newArrayList();
        }
        return collect.stream().map(CollectConvert::convertToCollectRelationPO).collect(Collectors.toList());
    }

    public static CollectSetConditionBO convertToCollectSetConditionBO(QueryCollectSetByConditionReq req) {
        if (req == null) {
            return null;
        }
        CollectSetConditionBO conditionBO = new CollectSetConditionBO();
        conditionBO.setPageIndex(req.getPageIndex());
        conditionBO.setPageSize(req.getPageSize());
        conditionBO.setTitle(req.getTitle());
        conditionBO.setProvince(req.getProvince());
        conditionBO.setCity(req.getCity());
        conditionBO.setCollectType(CollectSetTypeEnum.getByType(req.getCollectType()));
        conditionBO.setCreateUser(req.getCreateUser());
        conditionBO.setCurrentIndex(req.getCurrentIndex());
        return conditionBO;
    }

    public static PageResult<QueryCollectSetByConditionResp> convertToPageQueryCollectSetByConditionResp(PageResult<CollectSetBO> pageResult) {
        if (pageResult == null) {
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
        }
        PageResult<QueryCollectSetByConditionResp> result = new PageResult<>();
        result.setPageNum(pageResult.getPageNum());
        result.setPageSize(pageResult.getPageSize());
        result.setTotal(pageResult.getTotal());
        result.setPageCount(pageResult.getPageCount());
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setHasNext(pageResult.isHasNext());
        result.setResults(pageResult.getResults().stream().map(item -> {
            QueryCollectSetByConditionResp resp = new QueryCollectSetByConditionResp();
            resp.setCollectType(item.getCollectType() == null ? null : item.getCollectType().getType());
            resp.setCity(item.getCity());
            resp.setDesc(item.getDesc());
            resp.setStatus(item.getStatus() == null ? null : item.getStatus().getType());
            resp.setCode(item.getCode());
            resp.setId(item.getId());
            resp.setMainImg(item.getMainImg());
            resp.setProvince(item.getProvince());
            resp.setTitle(item.getTitle());
            return resp;
        }).collect(Collectors.toList()));
        return result;
    }

    public static List<CollectRelationBO> convertToCollectRelationBOList(UpdateCollectRelationReq req) {
        if (req == null) {
            return Lists.newArrayList();
        }
        List<CollectRelationBO> result = Lists.newArrayList();
        req.getCollectRelationList().forEach(item -> {
            CollectRelationBO collectRelationBO = new CollectRelationBO();
            collectRelationBO.setId(TodoCommonUtils.buildLongGuid());
            collectRelationBO.setRelationId(item.getRelationId());
            collectRelationBO.setRelationType(CollectSetTypeEnum.getByType(item.getRelationType()));
            collectRelationBO.setSort(item.getSort());
            collectRelationBO.setSetId(req.getSetId());
            result.add(collectRelationBO);
        });
        return result;
    }

    public static QueryCollectSetDetailResp convertToQueryCollectSetDetailResp(CollectSetBO collectSetBO, List<CollectRelationBO> collectRelationBOS) {
        if (collectSetBO == null) {
            return null;
        }
        QueryCollectSetDetailResp resp = new QueryCollectSetDetailResp();
        resp.setCollectType(collectSetBO.getCollectType() == null ? null : collectSetBO.getCollectType().getType());
        resp.setCity(collectSetBO.getCity());
        resp.setDesc(collectSetBO.getDesc());
        resp.setId(collectSetBO.getId());
        resp.setCode(collectSetBO.getCode());
        resp.setStatus(collectSetBO.getStatus() == null ? null : collectSetBO.getStatus().getType());
        resp.setMainImg(collectSetBO.getMainImg());
        resp.setProvince(collectSetBO.getProvince());
        resp.setCollectRule(collectSetBO.getCollectRule());
        resp.setTitle(collectSetBO.getTitle());
        resp.setRelationInfoList(collectRelationBOS.stream().map(item -> {
            QueryCollectSetDetailResp.RelationInfo relationInfo = new QueryCollectSetDetailResp.RelationInfo();
            relationInfo.setRelationType(item.getRelationType() == null ? null : item.getRelationType().getType());
            relationInfo.setRelationId(item.getRelationId());
            relationInfo.setSort(item.getSort());
            return relationInfo;
        }).collect(Collectors.toList()));
        return resp;
    }
}
